import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 用户的登录状态信息
    user: JSON.parse(window.localStorage.getItem('TOUTIAO_USER')),
    userdetail: {
      photo: null,
      name: 'damo',
      birthday: ' Feb 29 2000',
      gender: 0
    },
    userInfo: {
      name: 'damo',
      art_count: 13,
      follow_count: 0,
      fans_count: 112312,
      like_count: 1135124,
      photo: 't1.jpg'
    },
    textLike: [],
    autorInfo: [],
    autorlist: [
      {
        name: 'damo',
        art_count: 13,
        follow_count: 0,
        fans_count: 112312,
        like_count: 1135124,
        photo: 't1.jpg'
      },
      {
        name: 'mike',
        art_count: 3,
        follow_count: 1,
        fans_count: 13467,
        like_count: 533124,
        photo: 't3.jpg'
      },
      {
        name: 'jerry',
        art_count: 132,
        follow_count: 1,
        fans_count: 3552312,
        like_count: 11335524,
        photo: 't4.jpg'
      },
      {
        name: 'marry',
        art_count: 613,
        follow_count: 1,
        fans_count: 2312,
        like_count: 11324,
        photo: 't5.jpg'
      },
      {
        name: 'yimi',
        art_count: 123,
        follow_count: 1,
        fans_count: 82312,
        like_count: 535324,
        photo: 't6.jpg'
      }
    ],
    channellists: [
      {
        name: '推荐',
        id: 4
      },
      {
        name: '修仙',
        id: 5
      },
      {
        name: '同城创作',
        id: 6
      },
      {
        name: '武侠',
        id: 7
      },
      {
        name: '魔改小说',
        id: 8
      },
      {
        name: '异世界',
        id: 9
      },
      {
        name: '玄幻',
        id: 10
      },
      {
        name: '奇幻',
        id: 11
      }
    ],
    usechannellists: [
      {
        name: '最热',
        id: 1
      },
      {
        name: '最新',
        id: 2
      },
      {
        name: 'bilibili',
        id: 3
      }
    ],
    newtext: [],
    chath: [
      {
        name: 'yimi',
        message: [],
        date: '1:45'
      },
      {
        name: 'mike',
        message: [],
        date: '7:23'
      },
      {
        name: 'jerry',
        message: [],
        date: '14:11'
      },
      {
        name: 'marry',
        message: [],
        date: '12:24'
      }
    ],
    historytext: [],
    search: []
  },
  mutations: {
    setUser (state, user) {
      state.user = user
      window.localStorage.setItem('TOUTIAO_USER', JSON.stringify(user))
    },
    editUserDetail (state, userdetail) {
      state.userdetail = userdetail
    },
    addLikeText (state, text) {
      state.textLike.push(text)
    },
    removeLikeText (state, textid) {
      var i = state.textLike.findIndex(val => val.textid === textid)
      this.state.textLike.splice(i, 1)
    },
    addLikeAutor (state, autor) {
      state.userInfo.follow_count++
      state.autorlist.find(val => {
        if (val.name === autor.name) {
          val.fans_count++
          return true
        }
      })
      state.autorInfo.push(autor)
    },
    removeLikeAutor (state, name) {
      state.userInfo.follow_count--
      state.autorlist.find(val => {
        if (val.name === name) {
          val.fans_count--
          return true
        }
      })
      var i = state.autorInfo.findIndex(val => val.name === name)
      this.state.autorInfo.splice(i, 1)
    },
    addChannel (state, val) {
      state.usechannellists.push(val)
      var i = state.channellists.findIndex(item => item.id === val.id)
      this.state.channellists.splice(i, 1)
    },
    deleteChannel (state, val) {
      var i = state.usechannellists.findIndex(item => item.id === val.id)
      this.state.usechannellists.splice(i, 1)
      state.channellists.push(val)
    },
    addNewText (state, val) {
      state.newtext.push(val)
    },
    addChat (state, val) {
      var i = state.chath.findIndex(item => item.name === val.name)
      state.chath[i].message.push(val.message)
      var t = new Date()
      state.chath[i].date = t.getHours() + ':' + t.getMinutes()
    },
    addhistory (state, id) {
      if (state.historytext.indexOf(id) === -1) {
        state.historytext.push(id)
      }
    },
    addsearch (state, val) {
      if (state.historytext.indexOf(val) === -1) {
        state.search.push(val)
      }
    },
    removesearch (state, val) {
      var i = state.search.findIndex(item => item === val)
      this.state.search.splice(i, 1)
    }
  },
  actions: {
  },
  modules: {
  }
})
